LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY AUTO IS
  port(
        CLK: IN STD_LOGIC;
		  AUTO: IN STD_LOGIC;     --自动端口
        CLK2: buffer STD_LOGIC;
        INDEX2: IN STD_LOGIC_VECTOR(7 DOWNTO 0);
        INDEX0: OUT STD_LOGIC_vECTOR(7 DOWNTO 0));
		  
END AUTO;

ARCHITECTURE BEHAVIORAL OF AUTO IS
  SIGNAL COUNT0:INTEGER RANGE 0 TO 31;
BEGIN
PULSE0:PROCESS(CLK,AUTO)
  VARIABLE COUNT:INTEGER RANGE 0 To 2500000;
  BEGIN
    IF AUTO='1' THEN    --自动演奏开始，计数器清零，时钟低电平
       COUNT:=0;
		 CLK2<='0';
    ELS
	 IF(CLK'EVENT AND CLK='1') THEN  --时钟上升沿到来时，计数器开始计数
	    COUNT:=COUNT+1;
    IF COUNT=1250000 THEN      --分频得到1Hz的时钟，<4时是低电平，>4时是高电平
       CLK2<='1';
		 ELSIF COUNT=2500000 THEN   -- =8再次计数器清零，式中复位到低电平
	    CLK2<='0';
		 COUNT:=0;
    END IF;
	 END IF;
END PROCESS;


MUSIC:PROCESS(CLK2)    --计数器计31个数，应该是演奏31个音符
BEGIN
  IF(CLK2'EVENT AND CLK2='1') THEN
    IF(COUNT0=31) THEN
	   COUNT0<=0;
	 ELSE
	   COUNT0<=COUNT0+1;
	 END IF;
  END IF;
 END PROCESS;
--count0<=0;
 
 
 
 COM1:PROCESS(COUNT0,AUTO,INDEX2)
 BEGIN
--   IF AUTO='0' THEN
	  CASE COUNT0 IS     --弹的琴谱
	    WHEN 0=>INDEX0<="00000001";  --计数器计0，就给INDEX0赋值00000100，输出
       WHEN 1=>INDEX0<="00000001";
		 WHEN 2=>INDEX0<="00010000";
		 WHEN 3=>INDEX0<="00010000";
		 WHEN 4=>INDEX0<="00100000";
		 WHEN 5=>INDEX0<="00100000";
		 WHEN 6=>INDEX0<="00010000";
		 WHEN 7=>INDEX0<="00001000";
		 WHEN 8=>INDEX0<="00001000";
		 WHEN 9=>INDEX0<="00000100";
		 WHEN 10=>INDEX0<="00001000";
       WHEN 11=>INDEX0<="00000010";
		 WHEN 12=>INDEX0<="00000010";
		 WHEN 13=>INDEX0<="00000001";
		 WHEN 14=>INDEX0<="00000001";
		 WHEN 15=>INDEX0<="00000001";
		 WHEN 16=>INDEX0<="00010000";
		 WHEN 17=>INDEX0<="00010000";
		 WHEN 18=>INDEX0<="00001000";
		 WHEN 19=>INDEX0<="00001000";
		 WHEN 20=>INDEX0<="00001000";
       WHEN 21=>INDEX0<="00000100";
		 WHEN 22=>INDEX0<="00000010";
		 WHEN 23=>INDEX0<="00000010";
		 WHEN 24=>INDEX0<="00010000";
		 WHEN 25=>INDEX0<="00010000";
		 WHEN 26=>INDEX0<="00001000";
		 WHEN 27=>INDEX0<="00001000";
		 WHEN 28=>INDEX0<="00000100";
		 WHEN 29=>INDEX0<="00000100";
		 WHEN 30=>INDEX0<="00000010";
       WHEN 31=>INDEX0<="00000010";
		 WHEN OTHERS=>NULL;
     END CASE;
--	ELSE INDEX0<=INDEX2;
--	END IF;
END PROCESS;
END BEHAVIORAL;






















	 